<template>
  <div class="app-container">
    <div class="custom_card">
      <Tabs :list="tabs_list" :column="8" :active="activeIndex" @tabs_click="secondedTypeChange" calss="menuTabs"></Tabs>
    </div>
    <div class="custom_card_0" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="名称" prop="name" label-width="40px">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactsPeople" label-width="60px">
          <el-input v-model="queryParams.contactsPeople" placeholder="请输入联系人" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactsPhone" label-width="80px">
          <el-input v-model="queryParams.contactsPhone" placeholder="请输入联系电话" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button class="first-plain-btn" size="medium"
                     icon="el-icon-plus" @click="handleAdd"
            v-hasPermi="['supplier:supplier:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button class="sec-plain-btn" size="medium" icon="el-icon-download" :loading="exportLoading"
            @click="handleExport" v-hasPermi="['supplier:supplier:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleInfo(scope.row)"
              v-hasPermi="['supplier:supplier:edit']">查看</el-button>
            <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
              v-hasPermi="['supplier:supplier:edit']">修改</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              v-hasPermi="['supplier:supplier:remove']">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column label="供应商类型" align="center" prop="type" min-width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.supplier_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="名称" align="center" prop="name" min-width="100" :show-overflow-tooltip="true" />
        <el-table-column label="联系人" align="center" prop="contactsPeople" min-width="100" :show-overflow-tooltip="true" />
        <el-table-column label="联系电话" align="center" prop="contactsPhone" min-width="120" :show-overflow-tooltip="true" />
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="供应商类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择供应商类型" style="width: 100%">
                <el-option v-for="dict in dict.type.supplier_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="供应商名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="供应商联系人" prop="contactsPeople">
              <el-input v-model="form.contactsPeople" placeholder="请输入供应商联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="联系电话" prop="contactsPhone">
              <el-input v-model="form.contactsPhone" placeholder="请输入联系电话" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="供应商地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入供应商地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="addLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="600px" append-to-body>
      <el-row>
        <el-col :sm="24" :md="24" :lg="24" class="table-padding">
          <el-descriptions :column="2" border :labelStyle="{ width: '120px' }">
            <el-descriptions-item label-class-name="my-descriptions-label" span="2">
              <template slot="label"> 供应商名称</template>
              {{ form.name || '--' }}
            </el-descriptions-item>
            <el-descriptions-item label-class-name="my-descriptions-label">
              <template slot="label"> 供应商类型</template>
              <dict-tag :options="dict.type.supplier_type" :value="form.type" />
            </el-descriptions-item>
            <el-descriptions-item label-class-name="my-descriptions-label">
              <template slot="label"> 供应商联系人</template>
              {{ form.contactsPeople || '--' }}
            </el-descriptions-item>
            <el-descriptions-item label-class-name="my-descriptions-label">
              <template slot="label"> 联系电话</template>
              {{ form.contactsPhone || '--' }}
            </el-descriptions-item>
            <el-descriptions-item label-class-name="my-descriptions-label">
              <template slot="label"> 供应商地址</template>
              {{ form.address || '--' }}
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSupplier,
  getSupplier,
  delSupplier,
  addSupplier,
  updateSupplier,
  exportSupplier,
  statistics,
} from '@/api/supplier/supplier'
import Tabs from '@/components/custom/tabs.vue'
export default {
  name: 'Supplier',
  dicts: ['supplier_type'],
  data() {
    return {
      activeIndex: 0,
      tabs_list: [],
      // 遮罩层
      loading: true,
      addLoading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商表格数据
      supplierList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
        contactsPeople: null,
        contactsPhone: null,
        address: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules1: {
        type: [
          {
            required: true,
            message: '供应商类型不能为空',
            trigger: 'change',
          },
        ],
        name: [
          {
            required: true,
            message: '供应商名称不能为空',
            trigger: 'change',
          },
        ],
      },
      rules2: {
        type: [
          {
            required: true,
            message: '供应商类型不能为空',
            trigger: 'change',
          },
        ],
        name: [
          {
            required: true,
            message: '供应商名称不能为空',
            trigger: 'change',
          },
        ],
        contactsPhone: [
          {
            required: true,
            message: '手机号不能为空',
            trigger: 'change',
          },
          { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '手机号格式不正确,请重新输入', trigger: 'blur' }
        ],
      },
    }
  },
  computed: {
    rules() {
      if (this.form.type != null && this.form.type == '6') {
        return this.rules2;
      } else {
        return this.rules1;
      }
    }
  },

  components: {
    Tabs,
  },
  created() {
    this.getList()
    this.statistics()
  },
  methods: {
    statistics() {
      statistics(this.queryParams).then((response) => {
        if (!response.data) {
          this.tabs_list = [
            {
              key: '0',
              name: '全部',
              count: 0,
            },
            { key: '1', name: '车辆供应商', count: 0 },
            { key: '2', name: '汽车修理厂', count: 0 },
            { key: '4', name: '油卡公司', count: 0 },
            { key: '5', name: '所属公司', count: 0 },
            { key: '6', name: '托管车主', count: 0 },
          ]
          // this.statisticsNum = {
          //   gys: 0,
          //   xlc: 0,
          //   yk: 0,
          // }
        } else {
          this.tabs_list = [
            {
              key: '0',
              name: '全部',
              count:
                (response.data.gys || 0) +
                (response.data.xlc || 0) +
                (response.data.yk || 0) +
                (response.data.depositCarOwner || 0) +
                (response.data.ss || 0),
            },
            { key: '1', name: '车辆供应商', count: response.data.gys || 0 },
            { key: '2', name: '汽车修理厂', count: response.data.xlc || 0 },
            { key: '4', name: '油卡公司', count: response.data.yk || 0 },
            { key: '5', name: '所属公司', count: response.data.ss || 0 },
            { key: '6', name: '托管车主', count: response.data.depositCarOwner || 0 },
          ]
          // this.statisticsNum = {
          //   gys: response.data.gys || 0,
          //   xlc: response.data.xlc || 0,
          //   yk: response.data.yk || 0,
          // }
        }
      })
    },
    secondedTypeChange(type) {
      this.activeIndex = type || 0
      this.queryParams.type = type
      this.handleQuery()
    },
    /** 查询供应商列表 */
    getList() {
      this.loading = true
      listSupplier(this.queryParams).then((response) => {
        this.supplierList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openInfo = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        contactsPeople: null,
        contactsPhone: null,
        address: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.statistics()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加供应商'
    },
    /** 详情 */
    handleInfo(row) {
      this.reset()
      const id = row.id || this.ids
      getSupplier(id).then((response) => {
        this.form = response.data
        this.openInfo = true
        this.title = '供应商详情'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSupplier(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改供应商'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.addLoading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSupplier(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
                this.statistics()
                this.addLoading = false
              })
              .catch((re) => {
                this.addLoading = false
              })
          } else {
            addSupplier(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
                this.statistics()
                this.addLoading = false
              })
              .catch((re) => {
                this.addLoading = false
              })
          }
        } else {
          this.addLoading = false
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除供应商？')
        .then(function () {
          return delSupplier(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有供应商数据项？')
        .then(() => {
          this.exportLoading = true
          return exportSupplier(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => { })
    },
  },
}
</script>
